package com.likk.usermanage.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.likk.usermanage.mapper.UserMapper;
import com.likk.usermanage.pojo.EasyUIResult;
import com.likk.usermanage.pojo.User;
import com.likk.usermanage.service.UserService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public EasyUIResult queryEasyUIResult(Integer pageNum, Integer pageSize) {
		
		// 第一个参数是从那条开始，第二个参数是查多少条
		// List<User> userList = this.userMapper.queryUsersByPage((pageNum -1)* pageSize, pageSize);
		
		// 改造，使用分页插件进行查询
		// 在查询方法之前，调用分页插件的静态方法，中间最好不要隔任何代码
		PageHelper.startPage(pageNum, pageSize);
		// 调用查询方法
		List<User> userList = this.userMapper.queryUserAll();
		// 初始化pageInfo对象，所有的分页参数都可以在该对象中获取
		PageInfo<User> pageInfo = new PageInfo<>(userList);
		
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setTotal(pageInfo.getTotal());
		easyUIResult.setRows(pageInfo.getList());
		
		return easyUIResult;
	}

	@Override
	public void addUsers(User user1, User user2) throws Exception {
		this.userMapper.addUser(user1);
		// 制造异常
		int i=1/0;
		this.userMapper.addUser(user2);
		
	}
	
	@Override
	public Boolean addUser(User user) throws Exception {
		int count = this.userMapper.addUser(user);
		if(count>0){
			return true;
		}
		return false;
	}

	@Override
	public Boolean deleteUser(String[] ids) {
		
		int index = this.userMapper.deleteUser(ids);
		if(index > 0){
			return true;
		}
		
		return false;
	}

	@Override
	public User queryUserById(Long id) {

		User user = this.userMapper.queryUserById(id);
		
		return user;
	}

	@Override
	public void updateUser(User user) {

		this.userMapper.updateUser(user);
	}

	@Override
	public void deleteUserById(Long id) {
		
		this.userMapper.deleteUserById(id);
		
	}

	@Override
	public User queryUserByUsername(String username) {
		return this.userMapper.queryUserByUsername(username);
	}

}
